#import "SEGPaytradeManager.h"

#if __has_include(<Commom/SEGModel.h>)
#import <Commom/SEGModel.h>
#else
#import "SEGModel.h"
#endif
#if __has_include(<Commom/SEGHTTPBusinessManager.h>)
#import <Commom/SEGHTTPBusinessManager.h>
#else
#import "SEGHTTPBusinessManager.h"
#endif

static id _payTradeManager;
static dispatch_once_t onceToken;

@implementation SEGPaytradeManager
#pragma mark - SINGLE----BEGIN----
+(instancetype)sharedManager{
    dispatch_once(&onceToken, ^{
        _payTradeManager = [[self alloc] init];
    });
    return _payTradeManager;
}

+(void)destroyResource{
    onceToken = 0;
    _payTradeManager = nil;
}

#pragma mark - SINGLE----END----

#pragma mark - HTTP----BEGIN----
/**
 月卡缴费支付（有轮询）
 
 @author Sam
 */
-(void)requestPaytradeForPaymentPollByPaymenetResultWithRequestId:(NSString*)requestId
                                             payeeId:(NSString*)payeeId
                                            finished:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))finished{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:requestId forKey:@"requestId"];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-app/rest-api/v1/payment/pollByPaymenetResult.json",nil];
    [self requestPaytradeWithURLString:urlString params:params finished:finished];
}

/**
 提交购物支付信息
 
 @author Sam
 */
-(void)requestPaytradeForSubmitOrderPayWithPayMethod:(NSString*)payMethod
                                             orderId:(NSString*)orderId
                                             payeeId:(NSString*)payeeId
                                                                   finished:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))finished{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:orderId forKey:@"orderId"];
     [params setValue:payeeId forKey:@"payeeId"];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-app/rest-api/v1/payment/submitOrderPay.json",nil];
    [self requestPaytradeWithURLString:urlString params:params finished:finished];
}

/**
 临卡缴费
 
 @author Sam
 */
-(void)requestPaytradeForTemporaryCardPaymentNoCalculateChargeWithPayMethod:(NSString*)payMethod
                                                               finished:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))finished{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:payMethod forKey:@"payMethod"];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-app/rest-api/v1/parkingPay/temporaryCardPaymentNoCalculateCharge.json",nil];
    [self requestPaytradeWithURLString:urlString params:params finished:finished];
}

/**
 月卡缴费-不计算差
 
 @author Sam
 */
-(void)requestPaytradeForMonthCardPaymentNoCalculateChargeWithPayMethod:(NSString*)payMethod
                                         finished:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))finished{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:payMethod forKey:@"payMethod"];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-app/rest-api/v1/parkingPay/monthCardPaymentNoCalculateCharge.json",nil];
    [self requestPaytradeWithURLString:urlString params:params finished:finished];
}

/**
 侨城汇订单支付
 
 @author Sam
 */
-(void)requestPaytradeForThirdOrderPayWithOrderId:(NSString*)orderId
                                            payMethod:(NSString*)payMethod
                                             finished:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))finished{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:orderId forKey:@"orderId"];
    [params setValue:payMethod forKey:@"payMethod"];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-app/v1/thirdOrder/orderPay.json",nil];
    [self requestPaytradeWithURLString:urlString params:params finished:finished];
}

/**
 自助洗车订单支付
 
 @author Sam
 */
-(void)requestPaytradeForFlashboxOrderPayWithOrderSid:(NSString*)orderSid
                                        payMethod:(NSString*)payMethod
                                              finished:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))finished{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:orderSid forKey:@"orderSid"];
    [params setValue:payMethod forKey:@"payMethod"];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-app/flashbox-api/v1/orderPay.json",nil];
    [self requestPaytradeWithURLString:urlString params:params finished:finished];
}

/**
 物业服务提交支付订单
 
 @author Sam
 */
-(void)requestPaytradeForServicePayOrderWithPpayMethod:(NSString*)payMethod
                                  serviceOrderId:(NSString*)serviceOrderId
                                    organId:(NSString*)organId
                                      finished:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))finished{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:payMethod forKey:@"payMethod"];
    [params setValue:serviceOrderId forKey:@"serviceOrderId"];
    [params setValue:organId forKey:@"organId"];

    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/order-user-api/uhomecp-app/rest-api/v5/order/paid/servicePay.json",nil];
    [self requestPaytradeWithURLString:urlString params:params finished:finished];
}

/**
 费用缴纳账单支付
 
 @author Sam
 */
-(void)requestPaytradeForBillPaymentWithCustId:(NSString*)custId
                                     payAmount:(NSString*)payAmount
                                  disRuleIdStr:(NSString*)disRuleIdStr
                                    billAmount:(NSString*)billAmount
                                   acctItemIds:(NSString*)acctItemIds
                                       houseId:(NSString*)houseId
                                     payMethod:(NSString*)payMethod
                                      custType:(NSString*)custType
                                     disAmount:(NSString*)disAmount
                                       finished:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))finished{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:custId forKey:@"custId"];
    [params setValue:payAmount forKey:@"payAmount"];
    [params setValue:disRuleIdStr forKey:@"disRuleIdStr"];
    [params setValue:billAmount forKey:@"billAmount"];
    [params setValue:acctItemIds forKey:@"acctItemIds"];
    [params setValue:houseId forKey:@"houseId"];
    [params setValue:payMethod forKey:@"payMethod"];
    [params setValue:custType forKey:@"custType"];
    [params setValue:disAmount forKey:@"disAmount"];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-app/rest-api/v2/billPayment/getPayConfirmV2.json",nil];
    [self requestPaytradeWithURLString:urlString params:params finished:finished];
}

/**
 月卡缴费提交支付订单
 
 @author Sam
 @param payMethod 支付方式
 @param parkingPayField REDIS 缓存FIELD
 @param cycleLength 缴存周期数
 @param newExpDate 缴费后的失效时间
 @param paymentFee 优惠后的金额
 @param cardtype 卡类型。1 月卡， 2 季卡 3 半年卡，4 年卡，5 vip卡
 */
-(void)requestConfirmpaymentchargeWithPaymethod:(NSString*)payMethod
                                parkingPayField:(NSString*)parkingPayField
                                    cycleLength:(NSString*)cycleLength
                                     newExpDate:(NSString*)newExpDate
                                     paymentFee:(NSString*)paymentFee
                                       cardtype:(NSString*)cardtype
                                       finished:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))finished{

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:payMethod forKey:@"payMethod"];
    [params setValue:parkingPayField forKey:@"parkingPayField"];
    [params setValue:cycleLength forKey:@"cycleLength"];
    [params setValue:newExpDate forKey:@"newExpDate"];
    [params setValue:paymentFee forKey:@"paymentFee"];
    [params setValue:cardtype forKey:@"cardtype"];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/car-payment-api/rest-api/v1/parkingPayRestApiController/confirmPaymentCharge.json",nil];
    
    [self requestPaytradeWithURLString:urlString params:params finished:finished];
}

-(void)requestPaytradeWithURLString:(NSString*)URLString params:(NSDictionary*)params finished:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))finished{
    
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:URLString parameters:params completed:^(BOOL isSuccessed, id result, NSString *codeType, NSError *error) {
        if (!isSuccessed || result == nil) {
            finished(NO,nil,codeType,error.localizedDescription);
            return;
        }
        
        if (![result isKindOfClass:[NSDictionary class]]) {
            finished(NO,nil,codeType,error.localizedDescription);
            return;
        }
        
        self.payTrade = [SEGPaytradeModel yy_modelWithDictionary:result];
        finished(YES,result,codeType,error.localizedDescription);
        return;
    }];
}
#pragma mark - HTTP----END----

#pragma mark - GETTER----BEGIN----
-(SEGPaytradeModel *)payTrade{
    if (!_payTrade) {
        _payTrade = [[SEGPaytradeModel alloc]init];
    }
    return _payTrade;
}
#pragma mark - GETTER----END----
@end
